package com.huluobo.a2203parkdemo.notice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.databinding.ActivityAddNoticeBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class AddNoticeActivity : BaseActivity<ActivityAddNoticeBinding, NoticeViewModel>() {
    override fun initData() {
        viewModel.addNoticeSuccess.observe(this) {
            ToastUtils.showLong("发布成功")
            finish()
        }
    }

    override fun initView() {
        binding.addNoticeHeader.headerTitle.text = "发布新公告"
        binding.addNoticeHeader.headerBack.setOnClickListener { finish() }

        binding.addNoticeBtn.setOnClickListener {
            val titleString = binding.addNoticeTitleEt.text.toString()
            val valueString = binding.addNoticeValueEt.text.toString()
            if (titleString.isNotEmpty() && valueString.isNotEmpty()) {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_NOTICE_VALUE] = valueString
                map[Const.PARAM_NOTICE_TITLE] = titleString
                map[Const.PARAM_USER_ID] = 0

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.addNotice(body)
            } else {
                binding.addNoticeTitleEt.error = "标题或者内容不能为空"
            }

        }
    }
}